package com.zhkj.txg.module.product.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.txg.module.product.entity.CommentListResponse
import com.zhkj.txg.module.product.model.ProductManager

class CommentListViewModel(application: Application) : BaseViewModel(application) {
    private val manager = ProductManager()
    val commentListLiveData = MutableLiveData<HttpResponse<CommentListResponse>>()

    private val numb = 20
    var page = 1
        private set

    var type: Int = 0
    var goodsId: Long = 0

    fun commentList(restart: Boolean) {
        val listener = object : ViewModelSubscribeListener<CommentListResponse>(commentListLiveData) {
            override fun onSuccess(resp: CommentListResponse) {
                resp.hasMore = resp.data.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val commentList = manager.commentList(goodsId, type, page, numb, listener)
        dispose.add(commentList)
    }
}